package com.tlm.consumer;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class Config {
    @Bean//把对象里面的某个方法交给spring容器去管理
    @LoadBalanced//开启了ribbon的负载均衡
    public RestTemplate restTemplate(){
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpComponentsClientHttpRequestFactory.setConnectionRequestTimeout(1000);
        httpComponentsClientHttpRequestFactory.setConnectTimeout(1000);
        httpComponentsClientHttpRequestFactory.setReadTimeout(1000);
        return new RestTemplate(httpComponentsClientHttpRequestFactory);
//        return new RestTemplate();
    }


    //IRule是用来管理负载均衡算法的
    @Bean
    public IRule aaa(){
        return new RoundRobinRule();//return new  配置什么样的负载均衡算法
    }

}
